package com.lqd.mall.system.service.impl;

import com.lqd.mall.basis.exception.ExceptionCast;
import com.lqd.mall.interaction.system.SysDictTypeEntity;
import com.lqd.mall.jpa.service.impl.BaseServiceImpl;
import com.lqd.mall.system.repository.SysDictTypeRepository;
import com.lqd.mall.system.service.ISysDictDataService;
import com.lqd.mall.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author 零起点
 * @create 2022-03-21 18:02
 */
@Service
public class SysDictTypeServiceImpl
        extends BaseServiceImpl<SysDictTypeRepository, SysDictTypeEntity, Long>
        implements ISysDictTypeService {

    @Autowired
    ISysDictDataService sysDictDataService;

    @Override
    public Optional<Integer> updateEntity(SysDictTypeEntity entity, String... attribute) {
        if (entity.getDictId() < 1) {
            ExceptionCast.cast("更新必须传入id值");
        }
        SysDictTypeEntity previousValue = this.selectById(entity.getDictId());
        if (previousValue == null) {
            ExceptionCast.cast("字典不不存在");
        }

        if(!previousValue.getDictType().equals(entity.getDictType())){
            // 更新对应的类型值
            sysDictDataService.upateDictData(previousValue.getDictType(),entity.getDictType());
        }


        return super.updateEntity(entity, attribute);
    }
}
